/*NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN

	Copyright (c) 2009 Jounayd Id Salah.
	All rights reserved.

	This file is part of Blacklib.

	Blacklib is free software: you can redistribute it and/or modify it under the 
	terms of the GNU Lesser General Public License as published by the Free Software 
	Foundation, either version 3 of the License, or (at your option) any later version.
	Blacklib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
	without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
	PURPOSE.  See the GNU Lesser General Public License for more details.
	You should have received a copy of the GNU Lesser General Public License along with 
	Blacklib.  If not, see <http://www.gnu.org/licenses/>.
*/
//NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN

namespace black
{

	//__________________________________________________________________________
#ifdef bDEBUG
	sphere::sphere() : radius( F_NAN ) {}
#else
	sphere::sphere() {}
#endif

	//__________________________________________________________________________
	sphere::sphere( const vec3& vCenter, float fRadius ) : center( vCenter ), radius( fRadius )
	{
		assert( isValid() );
	}

	//__________________________________________________________________________
	bool sphere::contains( const vec3& v ) const
	{
		return center.squaredDistance( v ) <= radius*radius;
	}

	//__________________________________________________________________________
	bool sphere::contains( const sphere& v ) const
	{
		const float fDistMax	= radius - v.radius;
		if ( fDistMax < 0.f ) return false;

		return center.squaredDistance( v.center ) <= fDistMax*fDistMax;
	}

	//__________________________________________________________________________
	bool sphere::intersects( const sphere& v ) const
	{
		const float fDistMax = radius + v.radius;

		return center.squaredDistance( v.center ) <= fDistMax*fDistMax;
	}

	//__________________________________________________________________________
	bool sphere::isValid() const
	{
		return center.isValid() && black::isValid( radius ) && radius >= 0.f;
	}
}
